package com.member.web.controller.shop;

import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizUser;
import com.member.shop.service.ISysBizUserService;
import com.member.shop.vo.SysBizUserExportVo;
import com.member.shop.vo.SysBizUserVerifyVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【用户列】控制器 [会员团队核实列表]
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-01-03
 **/
@RestController
@RequestMapping("/shop/user/verify")
public class SysBizUserVerifyController extends BaseController {
    @Resource
    private ISysBizUserService sysBizUserService;

    /**
     * 查询 【用户会员团队核实列表】
     */
    @PreAuthorize("@ss.hasPermi('shop:userVerify:list')")
    @GetMapping("/list")
    public TableDataInfo listVerify(SysBizUser sysBizUser) {
        startPage();
        List<SysBizUserVerifyVo> list = sysBizUserService.listVerify(sysBizUser);
        return getDataTable(list);
    }

    /**
     * 核实【用户会员团队核实列表】
     */
    @PreAuthorize("@ss.hasPermi('shop:userVerify:edit')")
    @Log(title = "用户列表核实", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editVerify(@RequestBody SysBizUser sysBizUser) {
        SysBizUser sysBizUser1 = sysBizUserService.selectSysBizUserById(sysBizUser.getParentId());
        if(sysBizUser1.getAvailableMembers()==null || sysBizUser1.getAvailableMembers() <= 0){
            return error("核实失败，上级没有可发展会员人数");
        }
        return toAjax(sysBizUserService.editVerify(sysBizUser));
    }


    /**
     * 导出用户列列表
     */
    @PreAuthorize("@ss.hasPermi('shop:userVerify:export')")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizUser sysBizUser) {
        List<SysBizUserVerifyVo> list = sysBizUserService.listVerify(sysBizUser);
        if (!list.isEmpty()) {
            for (SysBizUserVerifyVo sysBizUserVerifyVo : list) {
                sysBizUserVerifyVo.setVerifyStatusName(sysBizUserVerifyVo.getVerifyStatus()==0?"未核实":"已核实");
            }
        }
        ExcelUtil<SysBizUserVerifyVo> util = new ExcelUtil<SysBizUserVerifyVo>(SysBizUserVerifyVo.class);
        util.exportExcel(response, list, "用户列数据");
    }
}
